package com.everyline.baselibrary.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.afollestad.materialdialogs.MaterialDialog;
import com.afollestad.materialdialogs.customview.DialogCustomViewExtKt;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.everyline.baselibrary.R;
import com.everyline.baselibrary.http.ApiException;
import com.everyline.baselibrary.interfaceListener.DialogButtonClickListener;
import com.everyline.baselibrary.utils.JActivityManager;

/**
 * crated by Administrator on 2021/6/3
 * 746608431@qq.com
 * 描述：TODO
 */
abstract public class BaseDBActivity<V extends ViewDataBinding> extends AppCompatActivity {
    protected V mViewDataBinding;
    protected Activity mContext;

    protected MaterialDialog loadingDialog;
    protected MaterialDialog dialogTwoButton;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        mViewDataBinding = DataBindingUtil.setContentView(this, getLayoutId());
        mViewDataBinding.setLifecycleOwner(this);
        JActivityManager.getInstance().addActivity(this);
        initView(savedInstanceState);
        initPresenter();
        initData();
        initLocation();
        initListener();
    }


    abstract protected int getLayoutId();

    abstract protected void initView(Bundle savedInstanceState);

    abstract protected void initPresenter();

    abstract protected void initData();

    protected void initLocation() {
    }


    protected abstract void initListener();


    @Override
    protected void onDestroy() {
        super.onDestroy();
        KeyboardUtils.hideSoftInput(this);
        JActivityManager.getInstance().closeActivity(this);
    }

    protected void showLoadingExt() {
        if (!this.isFinishing()) {
            if (loadingDialog == null) {
                loadingDialog = new MaterialDialog(this, MaterialDialog.getDEFAULT_BEHAVIOR());
                loadingDialog.cancelable(false);
                loadingDialog.cancelOnTouchOutside(false);
                loadingDialog.cornerRadius(12f, null);
                DialogCustomViewExtKt.customView(loadingDialog, R.layout.layout_custom_progress_dialog_view, null, true, false, false, true);
            }
            loadingDialog.show();

        }
    }

    protected void dismissLoadingExt() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }
    }


    protected void showDialogOneORTwoButton(String title, String message, String[] buttonNames, DialogButtonClickListener dialogButtonClickListener) {
        dialogTwoButton = new MaterialDialog(this, MaterialDialog.getDEFAULT_BEHAVIOR());
        dialogTwoButton.cancelable(false);
        dialogTwoButton.cancelOnTouchOutside(false);
        dialogTwoButton.cornerRadius(12f, null);
        DialogCustomViewExtKt.customView(dialogTwoButton, R.layout.dialog_two_button_layout, null, true, true, false, true);

        dialogTwoButton.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                TextView tv_dialog_title = DialogCustomViewExtKt.getCustomView(dialogTwoButton).findViewById(R.id.tv_dialog_title);
                TextView tv_dialog_message = DialogCustomViewExtKt.getCustomView(dialogTwoButton).findViewById(R.id.tv_dialog_message);
                TextView tv_dialog_cancle = DialogCustomViewExtKt.getCustomView(dialogTwoButton).findViewById(R.id.tv_dialog_cancle);
                TextView tv_dialog_ok = DialogCustomViewExtKt.getCustomView(dialogTwoButton).findViewById(R.id.tv_dialog_ok);
                View line_mid = DialogCustomViewExtKt.getCustomView(dialogTwoButton).findViewById(R.id.line_mid);
                tv_dialog_title.setText(title);
                tv_dialog_message.setText(message);
                if (buttonNames != null && buttonNames.length > 0) {
                    switch (buttonNames.length) {
                        case 2:
                            tv_dialog_cancle.setText(buttonNames[0]);

                            tv_dialog_cancle.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (dialogTwoButton != null) {
                                        dialogTwoButton.dismiss();
                                        dialogButtonClickListener.negativeButtonClick();
                                    }
                                }
                            });
                            tv_dialog_ok.setText(buttonNames[1]);
                            tv_dialog_ok.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (dialogTwoButton != null) {
                                        dialogTwoButton.dismiss();
                                        dialogButtonClickListener.positiveButtonClick();
                                    }
                                }
                            });
                            break;
                        case 1:

                            tv_dialog_cancle.setVisibility(View.GONE);
                            line_mid.setVisibility(View.GONE);
                            tv_dialog_ok.setText(buttonNames[0]);
                            tv_dialog_ok.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (dialogTwoButton != null) {
                                        dialogTwoButton.dismiss();
                                        dialogButtonClickListener.positiveButtonClick();
                                    }
                                }
                            });
                            break;
                    }
                }

            }
        });
        dialogTwoButton.show();
    }

    protected void handleThrowable(Throwable throwable) {
        throwable.printStackTrace();

        //todo  比如token 失效 或者封停用户 直接跳到 登录页   其他情况  可以在这里做统一处理

        if (throwable instanceof ApiException) {
            ApiException exception = (ApiException) throwable;
            String errorMsg = exception.getMsg();
            int err_code = exception.getCode();
            LogUtils.d("err_code===", err_code);
            if (err_code == 1001) {

            } else if (err_code == 0) {

            } else {
                if (!TextUtils.isEmpty(errorMsg)) {
                    ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).showShort(errorMsg);
                }
            }
        }
    }

    protected void showToast(String msg) {
        if (!StringUtils.isEmpty(msg)) {
            ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).showShort(msg);
        }

    }
}
